/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.ecarx.systemui.plugin.AppConstants;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.ui.dialog.base.AutoCloseDialog;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;
import com.ecarx.systemui.plugin.model.listener.NoDoubleClickListener;
import com.ecarx.systemui.plugin.utils.AppUtils;
import com.ecarx.systemui.plugin.utils.DensityUtils;
import com.ecarx.systemui.plugin.utils.LogUtils;

public class UsbDialog extends AutoCloseDialog {

    private static final String TAG = "UsbDialog";

    public static final int DIALOG_TOTAL_WIDTH = DensityUtils.dp2px(480);

    private int[] location;

    private ViewGroup rootView;

    public UsbDialog(@NonNull Context context, int[] location) {
        this(context, 0);
        this.location = location;
    }

    public UsbDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_usb);
        initView();
        initConfig();
    }

    private void initView() {
        rootView = findViewById(R.id.rootView);
        findViewById(R.id.iv_media).setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                AppUtils.startMedia(getContext(), true);
                delayedDismiss();
            }
        });
        findViewById(R.id.iv_gallery).setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                AppUtils.startGallery(getContext(), true);
                delayedDismiss();
            }
        });
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initConfig() {
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    AppConstants.PLUGIN_DIALOG_TYPE,
                    android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN |
                            android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                            android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                    ,
                    PixelFormat.TRANSPARENT);
            params.gravity = Gravity.START | Gravity.TOP;
            params.y = DensityUtils.dp2px(94);
            params.x = location[0];
            LogUtils.d(TAG, "params.x= " + location[0]);
            window.setAttributes(params);
            setCanceledOnTouchOutside(true);
        }
    }

    @Override
    protected void delayedDismiss() {
        dismiss();
    }

    @Override
    public void dismiss() {
        rootView.startAnimation(outAnimation);
        rootView.postDelayed(() -> {
            UsbDialog.super.dismiss();
            DialogManager.getDialogManager().cleanUsbDialog();
        }, 150);

    }

    public void updateConfig(int[] location) {
        this.location = location;
        initConfig();
    }

    public void showDialogAgain() {


    }
}
